#
# Copyright (C) [2024] Xingyun Integrated Circuit, Inc.
#
# GreenCode was a private technology asset of Xingyun Integrated Circuit， Inc （Confidential）
# Author: Shawn.Tan
# Date: 2025.10.28
#
# History: Initial Version 2025.10.28
#
#
from VectorTestSequence import VectorTestSequence
from gpgpu.EnvGPGPU import EnvGPGPU
from gpgpu.GenThreadGPGPU import GenThreadGPGPU


#  This test verifies that instructions with specific operand overlap
#  restrictions are generated
# legally.
class MainSequence(VectorTestSequence):
    def __init__(self, aGenThread, aName=None):
        super().__init__(aGenThread, aName)

        self._mInstrList = (
            "VCOMPRESS.VM##CALM",
            "VFSLIDE1UP.VF##CALM",
            "VIOTA.M##CALM",
            "VMSBF.M##CALM",
            "VMSIF.M##CALM",
            "VMSOF.M##CALM",
            "VRGATHER.VI##CALM",
            "VRGATHER.VV##CALM",
            "VRGATHER.VX##CALM",
            "VSLIDE1UP.VX##CALM",
            "VSLIDEUP.VI##CALM",
            "VSLIDEUP.VX##CALM",
        )

    # Return the maximum number of test instructions to generate.
    def _getMaxInstructionCount(self):
        return 1000

    # Return a list of test instructions to randomly choose from.
    def _getInstructionList(self):
        return self._mInstrList

    # Verify additional aspects of the instruction generation and execution.
    #
    #  @param aInstr The name of the instruction.
    #  @param aInstrRecord A record of the generated instruction.
    def _performAdditionalVerification(self, aInstr, aInstrRecord):
        vs2_val = aInstrRecord["Srcs"]["vs2"]
        vd_val = aInstrRecord["Dests"]["vd"]
        if vs2_val == vd_val:
            self.error("Instruction %s used overlapping source and destination registers" % aInstr)

        vs1_val = aInstrRecord["Srcs"].get("vs1")
        if vs1_val and (vs1_val == vd_val):
            self.error("Instruction %s used overlapping source and destination registers" % aInstr)

        vm_val = aInstrRecord["Imms"].get("vm")
        if vm_val and ((vm_val == 0) and (vd_val == 0)):
            self.error("Instruction %s is masked with v0 as the destination register" % aInstr)


MainSequenceClass = MainSequence
GenThreadClass = GenThreadGPGPU
EnvClass = EnvGPGPU
